//
//  YCNewFeatureViewController.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/17.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YCNewFeatureViewController: UICollectionViewController {

    
    let count = 4
    
    let layout = UICollectionViewFlowLayout()
    
    //因为是自定义类，而collectionViewController又需要调用init(collectionViewLayout: layout)才能创建成功
    init(){
    
    super.init(collectionViewLayout: layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addUI()
        
        
    }
    
    func addUI(){
        
        self.collectionView!.registerClass(YCCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //两边间距
        layout.minimumInteritemSpacing = 0
        //cell间距
        layout.minimumLineSpacing = 0
        
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //分页
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
        
    }
    
    //MARK:-代理方法


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YCCollectionCell
        cell.enjoyBtn.hidden = true
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //这2个有什么区别?
//        let index = collectionView.indexPathsForVisibleItems().last
//        let index1 = collectionView.indexPathsForVisibleItems()
//        print("\(index)")
//        print("\(index1)")
        let custoimIndexPath = collectionView.indexPathsForVisibleItems().last
        
        if let index = custoimIndexPath  {
            
            if index.item == count - 1{
                //这里要用这个方法才能显示你要的那个cell
                let myCell = collectionView.cellForItemAtIndexPath(index) as! YCCollectionCell
                myCell.startEnjoyBtnAnimation()
            }
        
        }
        

    }
    


}

//因为在别的类都不用用到这个类，所以就直接在这里写了
class YCCollectionCell: UICollectionViewCell{

  
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var index = 0 {
    
        //属性监视器
        didSet{
            bgkView.image = UIImage(named: "new_feature_\(index + 1)")
        
        }
    
    
    }
    
    func startEnjoyBtnAnimation(){
        enjoyBtn.hidden = false
        
        self.enjoyBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //变形恢复原形
            self.enjoyBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("动画完成")
        }
    
    }
    
    
    private func addUI(){
    
        contentView.addSubview(bgkView)
        contentView.addSubview(enjoyBtn)
        
        
        bgkView.translatesAutoresizingMaskIntoConstraints = false
        enjoyBtn.translatesAutoresizingMaskIntoConstraints = false
        
         addConstraint(NSLayoutConstraint(item: bgkView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bgkView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bgkView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bgkView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        
        
        addConstraint(NSLayoutConstraint(item: enjoyBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -100))
        addConstraint(NSLayoutConstraint(item: enjoyBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

        
    }
    
    private lazy var enjoyBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("立即体验", forState: UIControlState.Normal)
        btn.hidden = true
        
        return btn
    
    }()
    
    private lazy var bgkView: UIImageView = {
    
        let bg = UIImageView()
        bg.image = UIImage(named:"new_feature_1")
        
        return bg
    
    }()
    

}
